package com.xgk.boot.module.core.controller.admin.customer;

import com.xgk.boot.framework.common.core.ServerOption;
import com.xgk.boot.framework.common.pojo.CommonResult;
import com.xgk.boot.framework.common.pojo.PageResult;
import com.xgk.boot.framework.common.util.object.BeanUtils;
import com.xgk.boot.module.core.controller.admin.customer.vo.CustomerListVO;
import com.xgk.boot.module.core.controller.admin.customer.vo.CustomerReqVO;
import com.xgk.boot.module.core.controller.admin.customer.vo.CustomerSaveVO;
import com.xgk.boot.module.core.controller.admin.customer.vo.CustomerVO;
import com.xgk.boot.module.core.dal.entity.channel.ServiceProviderDO;
import com.xgk.boot.module.core.dal.entity.customer.CustomerDO;
import com.xgk.boot.module.core.service.customer.CustomerServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

import static com.xgk.boot.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 客户管理")
@RestController("adminCustomerController")
@RequestMapping("/system/customer")
public class CustomerController {

    @Resource
    private CustomerServiceImpl customerService;

    @PostMapping({"/save"})
    @Operation(summary = "保存新客户")
    @PreAuthorize("@ss.hasPermission('system:admin')")
    public CommonResult<CustomerVO> save(@Valid  @RequestBody CustomerSaveVO customerSaveVO) {
        CustomerVO customer = customerService.createOrUpdateCustomer(customerSaveVO);
        return CommonResult.success(customer);
    }

    @PostMapping({"/update"})
    @Operation(summary = "更新客户")
    @PreAuthorize("@ss.hasPermission('system:admin')")
    public CommonResult<CustomerVO> update(@Valid  @RequestBody CustomerSaveVO customerSaveVO) {
        CustomerVO customer = customerService.createOrUpdateCustomer(customerSaveVO);
        return CommonResult.success(customer);
    }

    @PutMapping({"/update/{id}/{status}"})
    @Operation(summary = "更新客户状态")
    @PreAuthorize("@ss.hasPermission('system:admin')")
    public CommonResult<Boolean> updateCustomerStatus(@PathVariable("id") Long id,@PathVariable("status") Integer status) {
        return CommonResult.success(customerService.updateCustomerStatus(id,status));
    }

    @GetMapping("/get/self")
    @Operation(summary = "获得用户自身详情")
    public CommonResult<CustomerListVO> getCustomerSelf() {
        CustomerListVO accountCustomer = customerService.getAccountCustomer();
        return  CommonResult.success(accountCustomer);
    }

    @GetMapping("/get/{id}")
    @Operation(summary = "获得用户详情")
    @Parameter(name = "id", description = "客户id", required = true, example = "1934923143484133377")
    @PreAuthorize("@ss.hasPermission('system:admin')")
    public CommonResult<CustomerVO> getCustomer(@PathVariable("id") Long id) {
        CustomerDO custCacheById = customerService.getCustCacheById(id);
        CustomerVO bean = BeanUtils.toBean(custCacheById, CustomerVO.class);
        return  CommonResult.success(bean);
    }

    @GetMapping("/get_by_code")
    @Operation(summary = "通过code获得用户详情")
    @Parameter(name = "code", description = "客户code", required = true, example = "C0001")
    @PreAuthorize("@ss.hasPermission('system:admin')")
    public CommonResult<CustomerVO> getCustByCode(@RequestParam("code") String code) {
        CustomerDO custCacheById = customerService.getCustCacheByCode(code);
        CustomerVO bean = BeanUtils.toBean(custCacheById, CustomerVO.class);
        return  CommonResult.success(bean);
    }

    @PostMapping("/page_list")
    @Operation(summary = "分页获得用户列表")
    @PreAuthorize("@ss.hasPermission('system:admin')")
    public CommonResult<PageResult<CustomerListVO>> pageList(@Valid @RequestBody CustomerReqVO custPageReqVO) {
        PageResult<CustomerDO> customerPageRes = customerService.pageCustomers(custPageReqVO);
        PageResult<CustomerListVO> bean = BeanUtils.toBean(customerPageRes, CustomerListVO.class);
        return CommonResult.success(bean);
    }

    @PostMapping("/page")
    @Operation(summary = "分页获得用户(金额)列表")
    @PreAuthorize("@ss.hasPermission('system:admin')")
    public CommonResult<PageResult<CustomerListVO>> pageListCustAndCost(@Valid @RequestBody CustomerReqVO custPageReqVO) {
        PageResult<CustomerListVO> customerPageRes = customerService.pageCustomersAndCost(custPageReqVO);
        return CommonResult.success(customerPageRes);
    }

    @PostMapping("/list")
    @Operation(summary = "获得用户列表")
    @PreAuthorize("@ss.hasPermission('system:admin')")
    public CommonResult<List<CustomerListVO>> list(@Valid @RequestBody CustomerReqVO customerReqVO) {
        List<CustomerDO> customerDOS = customerService.listCustomers(customerReqVO);
        return CommonResult.success(BeanUtils.toBean(customerDOS,CustomerListVO.class));
    }


    @DeleteMapping("/delete")
    @Operation(summary = "删除用户")
    @Parameter(name = "id", description = "客户id", required = true, example = "1934923143484133377")
    @PreAuthorize("@ss.hasPermission('system:admin')")
    public CommonResult<Boolean> deleteCustomer(@PathVariable("id") Long id) {
        return  CommonResult.success( customerService.deleteCustById(id));
    }


    @PutMapping("/reset-password/{id}")
    @Operation(summary = "重置客户密码")
    @Parameter(name = "id", description = "客户id", required = true, example = "1934923143484133377")
    @PreAuthorize("@ss.hasPermission('system:admin')")
    public CommonResult<Boolean> resetCustomerPassword(@PathVariable("id") Long id) {
        return  CommonResult.success( customerService.resetCustomerPassword(id));
    }

    @Operation(summary = "/查询客户列表")
    @GetMapping(value = "/selectList")
    public CommonResult<List<ServerOption>> getCustomerList() {
        List<CustomerDO> list = customerService.list();
        List<ServerOption> maps =
                list.stream().map(d -> new ServerOption<>(d.getId(),
                        d.getCustomerName() + "[" + d.getCustomerCode() + "]")).collect(Collectors.toList());
        return success(maps);
    }

}
